﻿#region Using directives
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;                       // DllImport()
using System.Threading;
using SmartFrequency.SoundAnalysis;
using SmartFrequency.SoundCapture;
using SmartFrequency.GUI;
using SmartFrequency.Utils;
#endregion

namespace SmartFrequency
{
    public partial class Form1 : Form
    {
        #region Private variables
        private Language.Language_e PLanguage = Language.Language_e.TR;
        private Language ProgramLanguage;
        private Settings PSettings;
        private Utilities PUtils = new Utilities();
        int l1eng, l5eng;
        #endregion

        #region Public variables
        #endregion

        #region Test functions
        private void Test_SoundRecord()
        {
            WaveIn wi = new WaveIn(22050, 1, 16);
            int numDevices;

            if ((numDevices = (int)wi.NumDevices()) < 1)
            {
                MessageBox.Show("FAILURE : Could not find sound device, program will not run");
                return;
            }

            if (Wave.MMSYSERR.NOERROR != wi.Preload(3000, 256 * 1024))
            {
                MessageBox.Show("FAILURE: Failed to preload buffers");
                return;
            }

            if (Wave.MMSYSERR.NOERROR != wi.Start())
            {
                MessageBox.Show("FAILURE: Failed to start recording");
                return;
            }

            // Waiting for 2 seconds...
            Thread.Sleep(2000);

            wi.Stop();

            String fileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
            fileName = Path.GetDirectoryName(fileName);
            fileName = Path.Combine(fileName, "test.wav");
            MessageBox.Show("Saving file test.wav");
            if (Wave.MMSYSERR.NOERROR != wi.Save(fileName))
            {
                MessageBox.Show("FAILURE: Failed to save file");
            }

            wi.Dispose();
        }

        private void Test_SoundConversion()
        {
            WaveIn wi = new WaveIn(22050, 1, 16);
            WaveProcess wp = new WaveProcess(60, 1600, 22050);
            int numDevices;
            //byte[] RecordedData;
            //short[] RecordedData;
            IntPtr RecordedData;
            double Freq;

            if ((numDevices = (int)wi.NumDevices()) < 1)
            {
                MessageBox.Show("FAILURE : Could not find sound device, program will not run");
                return;
            }

            if (Wave.MMSYSERR.NOERROR != wi.Preload(3000, 256 * 1024))
            {
                MessageBox.Show("FAILURE: Failed to preload buffers");
                return;
            }

            if (Wave.MMSYSERR.NOERROR != wi.Start())
            {
                MessageBox.Show("FAILURE: Failed to start recording");
                return;
            }

            // Waiting for 0.1 seconds...
            Thread.Sleep(100);

            wi.Stop();
            //wi.CopyAllTo(out RecordedData);

            DateTime Start = DateTime.Now;
            wi.PassTo(0, out RecordedData);
            Freq = wp.ProcessFreqData(RecordedData, wi.HeaderLength(0));
            TimeSpan Elapsed = DateTime.Now - Start;

            StringBuilder str = new StringBuilder();
            str.AppendFormat("{0} - {1} ms", Freq, Elapsed.TotalMilliseconds.ToString());
            MessageBox.Show(str.ToString());

            wi.Dispose();
        }
        #endregion

        #region Private functions
        #endregion

        #region Public functions
        public void ChangeDisplayingLanguage()
        {
            label1.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM0_LABEL1);
            label2.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM0_LABEL2);
            label3.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM0_LABEL3);
            label4.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM0_LABEL4);
            label5.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.FORM0_LABEL5);

            menuItem1.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.MENU1_TEXT);
            menuItem2.Text = ProgramLanguage.FrmMsgToString(Language.FormMsg_e.MENU3_TEXT);
        }
        #endregion

        #region Form functions
        public Form1()
        {
            InitializeComponent();

            this.Text = PUtils.Version;
            l1eng = label1.Left;
            l5eng = label5.Left;

            PSettings = new Settings("settings.txt");
            PSettings.Load();

            switch (PSettings.GetMember("language"))
            {
                default:
                case "tr":
                case "TR":
                    label1.Left += 25;
                    label5.Left += 25;

                    PLanguage = Language.Language_e.TR;
                    break;
                case "eng":
                case "ENG":
                    PLanguage = Language.Language_e.ENG;
                    break;
            }
            ProgramLanguage = new Language(PLanguage);
            ChangeDisplayingLanguage();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Language.Language_e temp;
            PSettings.Load();

            switch (PSettings.GetMember("language"))
            {
                default:
                case "tr":
                    temp = Language.Language_e.TR;
                    break;
                case "eng":
                    temp = Language.Language_e.ENG;
                    break;
            }

            if (PLanguage != temp)
            {
                PLanguage = temp;
                if (PLanguage == Language.Language_e.TR)
                {
                    label1.Left = l1eng + 25;
                    label5.Left = l5eng + 25;
                }
                else
                {
                    label1.Left = l1eng;
                    label5.Left = l5eng;
                }
                ProgramLanguage.SetLanguage(PLanguage);

                ChangeDisplayingLanguage();
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            About AboutBox = new About();
            AboutBox.Show();
        }

        private void VoiceRecorder_Click(object sender, EventArgs e)
        {
            VoiceRecorder VRecorder = new VoiceRecorder();
            VRecorder.Show();
        }

        private void Settings_Click(object sender, EventArgs e)
        {
            //GeneralSettings GSettings = new GeneralSettings();
            //GSettings.Show();
            MainSettings MSettings = new MainSettings();
            MSettings.Show();
        }

        private void Metronom_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(ProgramLanguage.FrmMsgToString(Language.FormMsg_e.UNDER_CONSTRUCTION));
            Metronom PMetronom = new Metronom();
            PMetronom.Show();
        }

        private void Spectrum_Click(object sender, EventArgs e)
        {
            MessageBox.Show(ProgramLanguage.FrmMsgToString(Language.FormMsg_e.UNDER_CONSTRUCTION));
        }
        #endregion
    }
}
